#
from typing import Dict
import numpy as np
import matplotlib.pyplot as plt

class RadarCore(object):
    @staticmethod
    def radar_eq(pt, freq, g, sigma, te, b, nf, loss, range_values):
        """
        Implements radar range equation in dB domain
        
        Parameters:
        pt (float): Peak transmit power (Watts)
        freq (float): Radar operating frequency (Hz)
        g (float): Antenna gain (dB)
        sigma (float): Radar cross section (m²)
        te (float): Effective noise temperature (Kelvin)
        b (float): Receiver bandwidth (Hz)
        nf (float): Noise figure (dB)
        loss (float): System losses (dB)
        range_values (array-like): Target ranges (meters)
        
        Returns:
        snr_db (ndarray): Signal-to-noise ratio in dB for each range
        """
        # 物理常数
        c = 3.0e8  # 光速 (m/s)
        # 波长计算
        lambda_ = c / freq
        # dB域转换
        p_peak_db = 10 * np.log10(pt)                # 峰值功率 (dBW)
        lambda_sqdb = 10 * np.log10(lambda_**2)       # 波长平方 (dB)
        sigmadb = 10 * np.log10(sigma)              # RCS (dBm²)
        four_pi_cub = 10 * np.log10((4 * np.pi)**3)  # (4π)^3 (dB)
        k_db = 10 * np.log10(1.38e-23)              # 玻尔兹曼常数 (dBW/K/Hz)
        te_db = 10 * np.log10(te)                    # 噪声温度 (dBK)
        b_db = 10 * np.log10(b)                      # 带宽 (dBHz)
        # 距离四次方项计算
        range_pwr4_db = 10 * np.log10(np.power(range_values, 4))
        # 雷达方程计算
        numerator = p_peak_db + 2*g + lambda_sqdb + sigmadb
        denominator = four_pi_cub + k_db + te_db + b_db + nf + loss + range_pwr4_db
        snr_db = numerator - denominator
        return snr_db
    

    @staticmethod
    def radar_eq_tau(pt, freq, g, sigma, te, nf, loss, range_values, snr):
        # 将天线增益、损耗和噪声系数转换为基数10
        gain = 10 ** (0.1 * g)
        loss_factor = 10 ** (0.1 * loss)
        F = 10 ** (0.1 * nf)

        # 计算波长
        lambda_ = 3e8 / freq

        # 计算分母
        den = pt * gain * gain * sigma * lambda_ ** 2

        # 计算分子并求解脉冲宽度tau
        tau = []
        for r in range_values:
            num = (4 * np.pi) ** 3 * 1.38e-23 * te * F * loss_factor * r ** 4 * snr
            tau_i = num / den
            tau.append(tau_i)
        return tau
    
    @staticmethod
    def snr_by_ref(SNRref_db, Lossp_db, Rref, R, tau_ref, tau, Sigmaref, Sigma):
        # ---------------------------
        # 2. 单位转换
        # ---------------------------
        # dB转换为线性域
        snrref = 10 ** (SNRref_db / 10)
        lossp = 10 ** (Lossp_db / 10)
        
        # ---------------------------
        # 3. 核心计算
        # ---------------------------
        # 计算距离比四次方
        rangeratio = (Rref / R) ** 4
        
        # 实现公式 (1.60)
        snr_linear = snrref * (tau / tau_ref) * (1.0 / lossp) * (Sigma / Sigmaref) * rangeratio
        
        # 转换为dB值
        snr_db = 10 * np.log10(snr_linear)
        return snr_db

    @staticmethod
    def power_aperture(snr, tsc, sigma, range_val, te, nf, loss, az_angle, el_angle):
        """
        计算搜索雷达的功率孔径积(PAP)
        
        参数:
        snr       : 信噪比 (dB)
        tsc       : 扫描时间 (秒)
        sigma     : 目标雷达截面积 (m²)
        range_val : 目标距离 (米)，可以是标量或数组
        te        : 有效噪声温度 (开尔文)
        nf        : 噪声系数 (dB)
        loss      : 系统损失 (dB)
        az_angle  : 方位搜索角度 (度)
        el_angle  : 俯仰搜索角度 (度)
        
        返回:
        PAP       : 功率孔径积 (dB)
        """
        Tsc = 10 * np.log10(tsc)           # 扫描时间转dB
        Sigma = 10 * np.log10(sigma)        # RCS转dB
        four_pi = 10 * np.log10(4.0 * np.pi)  # 4π的dB值
        k_db = 10 * np.log10(1.38e-23)     # 玻尔兹曼常数(dB)
        Te = 10 * np.log10(te)             # 噪声温度转dB
        
        # 处理标量或数组形式的距离
        range_pwr4_db = 10 * np.log10(np.array(range_val)**4)  # R^4转dB
        
        # 计算搜索立体角(球面度)
        omega = az_angle * el_angle / (57.296)**2
        Omega = 10 * np.log10(omega)       # 立体角转dB
        
        # 实现雷达方程
        PAP = (snr + four_pi + k_db + Te + nf + loss + 
            range_pwr4_db + Omega - Sigma - Tsc)
        
        return PAP
    
    @staticmethod
    def pulse_integration(pt, freq, g, sigma, te, b, nf, loss, range_val, n_P, ci_nci):
        """
        计算脉冲累积后的信噪比(SNR)
        
        参数:
            pt: 峰值功率 (W)
            freq: 雷达频率 (Hz)
            g: 天线增益 (dB)
            sigma: 目标截面积 (m²)
            te: 有效噪声温度 (K)
            b: 带宽 (Hz)
            nf: 噪声系数 (dB)
            loss: 系统损耗 (dB)
            range_val: 目标距离 (m) 可以是标量或数组
            n_P: 脉冲数
            ci_nci: 累积类型 (1=相参累积, 2=非相参累积)
            
        返回:
            snrout: 累积后的SNR (dB)
        """
        # 计算单脉冲SNR
        snr1 = RadarCore.radar_eq(pt, freq, g, sigma, te, b, nf, loss, range_val)
        
        if ci_nci == 1:  # 相参累积
            snrout = snr1 + 10 * np.log10(n_P)
        
        elif ci_nci == 2:  # 非相参累积
            # 将SNR转换为线性值
            snr_nci = 10**(snr1 / 10)
            
            # 计算中间项
            val1 = (snr_nci**2) / (4 * n_P**2)
            val2 = snr_nci / n_P
            val3 = snr_nci / (2 * n_P)
            
            # 计算累积后的SNR (公式1.87)
            SNR_1 = val3 + np.sqrt(val1 + val2)
            
            # 计算非相参累积损失 (公式1.85)
            LNCI = (1 + SNR_1) / SNR_1
            
            # 计算最终SNR
            snrout = snr1 + 10 * np.log10(n_P) - 10 * np.log10(LNCI)
        
        return snrout
    